package com.ricky.base.util;


import com.ricky.base.bean.StreamGobbler;
import com.ricky.base.listener.TerminalListener;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Ricky
 * @date 2020/11/12
 */
public class TerminalUtil {

    public static int exec(String commandStr) throws IOException, InterruptedException {
        return exec(commandStr, ".");
    }

    public static int exec(String commandStr, String path) throws IOException, InterruptedException {
        return exec(commandStr, new File(path));
    }

    public static int exec(String commandStr, File dir) throws IOException, InterruptedException {
        System.out.println("执行命令：" + commandStr);
        Process process = Runtime.getRuntime().exec(commandStr, null, dir);

        StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
        StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), "OUTPUT");

        errorGobbler.start();
        outputGobbler.start();
        int exitVal = process.waitFor();
        System.out.println("命令退出代码: " + exitVal);
        return exitVal;
    }

    public static int execWithListener(String commandStr, TerminalListener terminalListener) throws IOException, InterruptedException {
        System.out.println("执行命令：" + commandStr);
        Process process = Runtime.getRuntime().exec(commandStr);

        StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), terminalListener, "ERROR");
        StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), terminalListener, "OUTPUT");

        errorGobbler.start();
        outputGobbler.start();
        int exitVal = process.waitFor();
        System.out.println("命令退出代码: " + exitVal);
        return exitVal;
    }

    public static String execToString(String commandStr) throws IOException, InterruptedException {
        System.out.println("执行命令：" + commandStr);
        Process process = Runtime.getRuntime().exec(commandStr);
        StringBuilder sb = new StringBuilder();

        try (InputStreamReader isr = new InputStreamReader(process.getInputStream(), "GBK")) {
            try (BufferedReader br = new BufferedReader(isr)) {
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line).append("\n");
                }
            }
        }

        int exitVal = process.waitFor();
        System.out.println("命令退出代码: " + exitVal);
        return sb.toString();
    }

    public static List<String> execToList(String commandStr) throws IOException, InterruptedException {
        System.out.println("执行命令：" + commandStr);
        Process process = Runtime.getRuntime().exec(commandStr);
        List<String> res = new ArrayList<>();

        try (InputStreamReader isr = new InputStreamReader(process.getInputStream(), "GBK")) {
            try (BufferedReader br = new BufferedReader(isr)) {
                String line;
                while ((line = br.readLine()) != null) {
                    res.add(line);
                }
            }
        }

        int exitVal = process.waitFor();
        System.out.println("命令退出代码: " + exitVal);
        return res;
    }
}
